package com.example.lvsuo.aop;



import com.example.lvsuo.entity.cxg.vo.SystemLogVo;

import io.netty.util.internal.StringUtil;
import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


import javax.servlet.http.HttpServletRequest;


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


@Aspect
@Component
public class AopLoggerAspect {

    @Autowired
    RedisTemplate redisTemplate;

//   public static    ThreadLocal th = new ThreadLocal();

//    @Autowired
//    SqlSessionFactory sqlSessionFactory;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    @Pointcut("execution(* com.example.lvsuo.controller..*.*(..))")
    public void aopLoggerAspect() {

    }

    /**
     * 方法后触发
     *
     * @param point
     * @return
     */
    @Around("aopLoggerAspect()")
    public Object doAround(ProceedingJoinPoint point)  {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = servletRequestAttributes.getRequest();// 获取requst
        Object result = null;
        long startTime = System.currentTimeMillis(); //开始时间
        try {
            result = point.proceed();//返回值
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            logger.error(throwable.getMessage());//否则异常
        }



//        Map<String, Object> logMap = new HashMap();
//
//        logMap.put("url",request.getRequestURL());// 请求资源路径
//        logMap.put("api",request.getRequestURI());// API接口
//        logMap.put("method",request.getMethod());// 请求的方法
//        logMap.put("funPath",point.getSignature().getDeclaringTypeName());// 调用方法路径
//        logMap.put("fun", point.getSignature().getName()); //调用的方法
//        logMap.put("ip",request.getRemoteAddr()); // 请求者的IP
//        logMap.put("parmatr",point.getArgs()); // 请求入参
//
//        logMap.put("Ginseng",result); //请求出参
//        logMap.put("requestDate",System.currentTimeMillis() - startTime); // 请求时间
//        logMap.put("startDate",LocalDateTime.now().getYear()+"年   "+LocalDateTime.now().getMonthValue()+"月   "+LocalDateTime.now().getDayOfMonth()+"日    "+LocalDateTime.now().getHour()+":"+LocalDateTime.now().getMinute()+":"+LocalDateTime.now().getSecond()); //请求日期

//        执行的sql
//        Object sqls = ServiceAspect.sThreadLocal.get();

//        System.out.println("获取的sql==>"+sqls);

        SystemLogVo logVo = new SystemLogVo();


        String apis = request.getRequestURI();


        String api = apis.split("/")[1];

//        System.out.println("一级api为=="+api);

//        logVo.setSql(sqls);
//        logVo.setUrl(request.getRequestURL());
        logVo.setApi(api);
        logVo.setMethod(request.getMethod());
//        logVo.setFunPath(point.getSignature().getDeclaringTypeName());
//        logVo.setFun(point.getSignature().getName());
//        logVo.setIp(request.getRemoteAddr());
//        logVo.setParmatr(point.getArgs());
//        logVo.setGinseng(result);
        logVo.setRequestDate(System.currentTimeMillis() - startTime);
//        logVo.setStartDate(LocalDateTime.now().getYear()+"年   "+LocalDateTime.now().getMonthValue()+"月   "+LocalDateTime.now().getDayOfMonth()+"日    "+LocalDateTime.now().getHour()+":"+LocalDateTime.now().getMinute()+":"+LocalDateTime.now().getSecond());
        logVo.setStartDate(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

//        System.out.println("缓存数据有:"+logVo);


        redisTemplate.opsForList().rightPush("monitorAPI",logVo);// 将日志数据放入redis中


        return result;
    }



}